package com.deyou.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.event.DebookRulesEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NormalDebookRulesActivity extends BaseActivity {

    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_debook_day)
    TextView tvDebookDay;
    @BindView(R.id.tv_debook_rate)
    TextView tvDebookRate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_debook_rules);
        ButterKnife.bind(this);
        tvMidTitle.setText("标准退订政策");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        tvRight.setTextColor(getResources().getColor(R.color.color_FF832D));
    }

    @OnClick({R.id.iv_left_back, R.id.tv_right, R.id.tv_debook_day, R.id.tv_debook_rate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.tv_right:
                if (TextUtils.isEmpty(tvDebookDay.getText().toString())){
                    showToast("请选择您的可提前天数");
                    return;
                }
                if (TextUtils.isEmpty(tvDebookRate.getText().toString())){
                    showToast("请选择您的扣款百分比");
                    return;
                }
                DebookRulesEvent event = new DebookRulesEvent(1, tvDebookDay.getText().toString().replace("天", ""),
                        tvDebookRate.getText().toString().replace("%", ""), "", true);
                EventBus.getDefault().post(event);
                finish();
                break;
            case R.id.tv_debook_day:
                onDayOptionPicker();
                break;
            case R.id.tv_debook_rate:
                onRateOptionPicker();
                break;
        }
    }

    public void onDayOptionPicker() {
        final List<String> days = new ArrayList<>();
        for (int i = 1; i < 32; i++){
            days.add(i + "天");
        }

        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvDebookDay.setText(days.get(options1));
            }
        })

                .setTitleText("")
                .setSelectOptions(5)
                .setDividerColor(getResources().getColor(R.color.color_E5E5E5))
                .setTextColorCenter(getResources().getColor(R.color.color_272424)) //设置选中项文字颜色
                .setSubmitColor(getResources().getColor(R.color.color_FF832D))
                .setCancelColor(getResources().getColor(R.color.color_959090))
                .setSubCalSize(14)//确定和取消文字大小
                .setContentTextSize(16)
                .setOutSideCancelable(false)// default is true
                .build();

        pvOptions.setPicker(days);//一级选择器
        pvOptions.show();
    }

    public void onRateOptionPicker() {
        final List<String> days = new ArrayList<>();
        for (int i = 1; i < 101; i++){
            days.add(i + "%");
        }
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvDebookRate.setText(days.get(options1));
            }
        })

                .setTitleText("")
                .setSelectOptions(19)
                .setDividerColor(getResources().getColor(R.color.color_E5E5E5))
                .setTextColorCenter(getResources().getColor(R.color.color_272424)) //设置选中项文字颜色
                .setSubmitColor(getResources().getColor(R.color.color_FF832D))
                .setCancelColor(getResources().getColor(R.color.color_959090))
                .setSubCalSize(14)//确定和取消文字大小
                .setContentTextSize(16)
                .setOutSideCancelable(false)// default is true
                .build();

        pvOptions.setPicker(days);//一级选择器
        pvOptions.show();
    }
}
